package com.sgb.zhaocai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.MarketCompanyEntitly;
import com.sgb.zhaocai.view.fragment.PickMarketCompanyFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PickMarketCompanyModel extends ViewModel {
    PickMarketCompanyFragment fragment;
    private boolean isLastPage;
    private int page = 1;

    public void setActivity(PickMarketCompanyFragment pickMarketCompanyFragment) {
        this.fragment = pickMarketCompanyFragment;
    }

    /**
     * 市场客户-企业
     * @param isLoadMore
     * @param companyName
     * @param fromWhere
     * @param no
     * @param province
     * @param city
     */
    public void getBiddingCompanyList(boolean isLoadMore, String companyName, String fromWhere, String no, String province, String city,String companynature,String rangeband) {
        page = isLoadMore? page+1:1;
        Map map = new HashMap();
        map.put("limit","20");
        map.put("page",page);
        map.put("selectCompanyName",companyName);//搜索
        map.put("companyNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,""));
        map.put("type",fromWhere);
        map.put("tenderNo",no);//标no
        map.put("provinceNo",province);
        map.put("cityNo",city);
        map.put("companyType",companynature);//企业性质
        map.put("distance",rangeband);//距离范围
        map.put("invitedType","1");
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getBiddingCompanyList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<MarketCompanyEntitly>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MarketCompanyEntitly>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                   /* isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();*/
                    for (MarketCompanyEntitly memberCompanyEntitly : data.getData().getList()) {
                        memberCompanyEntitly.setCheck(false);
                    }
                    fragment.setBiddingCompanyList(data.getData().getList(),data.getData().getTotal());
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }

    /**
     * 获取地址信息
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressList(data.getData());
                }
            }
        });

    }

    /**
     * 获取企业性质
     */
    public void getCompanyNatureList() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getCompanyTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    List<CommonPopEntity> datas = new ArrayList<>();
                    CommonPopEntity bean = new CommonPopEntity();
                    bean.setNo("");
                    bean.setTypeName("全部");
                    datas.add(bean);
                    datas.addAll(data.getData());
                    fragment.setCompanyNatureList(datas);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 获取距离范围
     */
    public void getRangeBandList() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getFindDistanceSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK))
                    fragment.setRangeBandList(data.getData());
                else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 获取对应订单地址
     * @param id
     * @param fromWhere
     */
    public void getBiddingRegionAddressModel(String id, String fromWhere) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getbiddingRegionAddressList(id,fromWhere).enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressList(data.getData());
                }
            }
        });
    }
}
